using System;
using System.Collections.Generic;
using Volo.Abp.Domain.Entities.Auditing;
using Volo.Abp.MultiTenancy;
using TenantManagementService.Monitoring;

namespace TenantManagementService.Monitoring
{
    /// <summary>
    /// 监控指标实体
    /// </summary>
    public class MonitoringMetric : FullAuditedAggregateRoot<Guid>, IMultiTenant
    {
        /// <summary>
        /// 租户ID
        /// </summary>
        public virtual Guid? TenantId { get; protected set; }

        /// <summary>
        /// 指标名称
        /// </summary>
        public virtual string Name { get; protected set; } = string.Empty;

        /// <summary>
        /// 指标显示名称
        /// </summary>
        public virtual string DisplayName { get; protected set; } = string.Empty;

        /// <summary>
        /// 指标描述
        /// </summary>
        public virtual string? Description { get; protected set; }

        /// <summary>
        /// 指标类型
        /// </summary>
        public virtual MetricType Type { get; protected set; }

        /// <summary>
        /// 指标类别
        /// </summary>
        public virtual MetricCategory Category { get; protected set; }

        /// <summary>
        /// 单位
        /// </summary>
        public virtual MetricUnit Unit { get; protected set; }

        /// <summary>
        /// 标签（JSON格式）
        /// </summary>
        public virtual string? Labels { get; protected set; }

        /// <summary>
        /// 是否启用
        /// </summary>
        public virtual bool IsEnabled { get; protected set; } = true;

        /// <summary>
        /// 最后更新时间
        /// </summary>
        public virtual DateTime? LastUpdateTime { get; protected set; }

        /// <summary>
        /// 关联的指标值
        /// </summary>
        public virtual ICollection<MonitoringMetricValue> MetricValues { get; protected set; } = new List<MonitoringMetricValue>();

        protected MonitoringMetric() { }

        public MonitoringMetric(
            Guid id,
            string name,
            string displayName,
            MetricType type,
            MetricCategory category,
            MetricUnit unit = MetricUnit.None,
            string? description = null,
            string? labels = null,
            Guid? tenantId = null) : base(id)
        {
            Name = Check.NotNullOrWhiteSpace(name, nameof(name), maxLength: 200);
            DisplayName = Check.NotNullOrWhiteSpace(displayName, nameof(displayName), maxLength: 200);
            Type = type;
            Category = category;
            Unit = unit;
            Description = description;
            Labels = labels;
            TenantId = tenantId;
            IsEnabled = true;
        }

        /// <summary>
        /// 更新基本信息
        /// </summary>
        public virtual MonitoringMetric UpdateBasicInfo(
            string displayName,
            string? description = null,
            string? labels = null)
        {
            DisplayName = Check.NotNullOrWhiteSpace(displayName, nameof(displayName), maxLength: 200);
            Description = description;
            Labels = labels;
            LastUpdateTime = DateTime.Now;
            return this;
        }

        /// <summary>
        /// 更新状态
        /// </summary>
        public virtual MonitoringMetric UpdateStatus(bool isEnabled)
        {
            IsEnabled = isEnabled;
            LastUpdateTime = DateTime.Now;
            return this;
        }

        /// <summary>
        /// 添加指标值
        /// </summary>
        public virtual MonitoringMetric AddValue(decimal value, DateTime timestamp, Dictionary<string, string>? dimensions = null)
        {
            var metricValue = new MonitoringMetricValue(
                Guid.NewGuid(),
                Id,
                value,
                timestamp,
                dimensions
            );

            MetricValues.Add(metricValue);
            LastUpdateTime = DateTime.Now;
            return this;
        }

        /// <summary>
        /// 获取最新的指标值
        /// </summary>
        public virtual MonitoringMetricValue? GetLatestValue()
        {
            return MetricValues.Count > 0 ?
                MetricValues.OrderByDescending(v => v.Timestamp).First() :
                null;
        }

        /// <summary>
        /// 获取指定时间范围内的指标值
        /// </summary>
        public virtual IEnumerable<MonitoringMetricValue> GetValuesByTimeRange(DateTime startTime, DateTime endTime)
        {
            return MetricValues
                .Where(v => v.Timestamp >= startTime && v.Timestamp <= endTime)
                .OrderBy(v => v.Timestamp);
        }
    }
}